import UIKit

//var greeting = "Hello, playground"
//
//
//// 使用 /.../ 语法定义正则表达式，避免了字符串转义的麻烦。
//let regex = /[a-zA-Z]+@[a-zA-Z]+\.[a-zA-Z]{2,}/
//let text = "Contact: support@example.com"
//if let match = text.firstMatch(of: regex) {
//    print("匹配到的邮箱地址：\(match)")
//} else {
//    print("没有匹配到邮箱地址")
//}

// 匹配和捕获组
//let text = "Email: alice@example.com"
//let regex = /(\w+)@(\w+)\.(\w+)/
//
//if let match = text.firstMatch(of: regex) {
//    let (fullMatch, username, domain, tld) = match.output
//    print("Username: \(username), Domain: \(domain), TLD: \(tld)")
//}

//// 命名捕获组
//let text = "Email: alice@example.com"
//let regex = /(?<username>\w+)@(?<domain>\w+)\.(?<tld>\w+)/
//if let match = text.firstMatch(of: regex) {
//    print("Username: \(match.username), Domain: \(match.domain), TLD: \(match.tld)")
//}
//
////替换
////使用 `replacing(_:with:)` 替换匹配。
//let result = text.replacing(regex, with: "[REDACTED]")
//print(result)


let regex = /[a-z]+/
let text = "Hello World"
let matches = text.matches(of: regex)
for match in matches{
    print("匹配:\(match.output)")
}
